package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressAddNewParam;
import cn.tedu.tmall.front.mall.pojo.vo.CartListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressListItemVO;
import cn.tedu.tmall.front.mall.service.ICartService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "3.购物车管理")
@RestController
@RequestMapping("/carts")
public class CartController {
    @Autowired
    private ICartService service;

    @ApiOperation("添加商品到购物车")
    @ApiOperationSupport(order = 100)
    @GetMapping("/add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true, dataType = "long"),
            @ApiImplicitParam(name = "goodsNum",value = "商品数量",required = true, dataType = "int")
    })
    public JsonResult add(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal,
                          @Range(min = 1,message = "请提供有效的商品id")Long goodsId,
                          @Range(min = 1,message = "请提供有效的商品数量")Integer goodsNum){
        service.add(principal, goodsId, goodsNum);
        return JsonResult.ok();
    }

    @ApiOperation("删除购物车中的商品")
    @ApiOperationSupport(order = 110)
    @GetMapping("/delete")
    @ApiImplicitParam(name = "goodsId",value = "商品id",required = true, dataType = "long")
    public JsonResult delete(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal, Long goodsId){
        service.deleteByUserIdGoodsID(principal, goodsId);
        return JsonResult.ok();
    }

    @ApiOperation("将购物车中的商品数量增加1")
    @ApiOperationSupport(order = 120)
    @GetMapping("/increase")
    @ApiImplicitParam(name = "goodsId",value = "商品id",required = true, dataType = "long")
    public JsonResult increase(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal, Long goodsId){
        Integer goodsNum = service.increase(principal, goodsId);
        return JsonResult.ok(goodsNum);
    }

    @ApiOperation("将购物车中的商品数量增加num")
    @ApiOperationSupport(order = 121)
    @GetMapping("/increase-num")
    @ApiImplicitParam(name = "goodsId",value = "商品id",required = true, dataType = "long")
    public JsonResult increase(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal, Long goodsId ,Integer num){
        Integer goodsNum = service.increase(principal, goodsId, num);
        return JsonResult.ok(goodsNum);
    }

    @ApiOperation("将购物车中的商品数量减少1")
    @ApiOperationSupport(order = 122)
    @GetMapping("/reduce")
    @ApiImplicitParam(name = "goodsId",value = "商品id",required = true, dataType = "long")
    public JsonResult reduce(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal, Long goodsId){
        Integer goodsNum = service.reduce(principal, goodsId);
        return JsonResult.ok(goodsNum);
    }

    @ApiOperation("将购物车中的商品数量减少num")
    @ApiOperationSupport(order = 123)
    @GetMapping("/reduce-num")
    @ApiImplicitParam(name = "goodsId",value = "商品id",required = true, dataType = "long")
    public JsonResult reduce(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal, Long goodsId, Integer num){
        Integer goodsNum = service.reduce(principal, goodsId, num);
        return JsonResult.ok(goodsNum);
    }

    @ApiOperation("查询购物车中的商品信息")
    @ApiOperationSupport(order = 131)
    @GetMapping("/list")
    public JsonResult listByUserId(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal){
        List<CartListItemVO> list = service.list(principal);
        return JsonResult.ok(list);
    }
}
